﻿Imports System.Web.HttpContext
Imports System.Web.Configuration

Public Class XmlPageMappingsController
    Inherits BasePageMappingsController


    Private Function GetFilePath() As String

        Return Current.Server.MapPath(WebConfigurationManager.AppSettings("PageMappingsXmlFilePath"))

    End Function



    Public Overrides Function GetHtmlUrl(ByVal silverlightUrl As String) As String

        Dim xdoc = XDocument.Load(GetFilePath())


        Dim mappings = From mapping In xdoc.<PageMappings>.<PageMapping> _
                       Where silverlightUrl.ToLowerInvariant.EndsWith(mapping.@silverlightUrl.ToLowerInvariant) _
                       Order By mapping.@order Ascending _
                       Select mapping.@htmlUrl

        Return FormatUrl(silverlightUrl, mappings)

    End Function

    Public Overrides Function GetSilverlightUrl(ByVal htmlUrl As String) As String

        Dim xdoc = XDocument.Load(GetFilePath())

        Dim mappings = From mapping In xdoc.<PageMappings>.<PageMapping> _
                       Where htmlUrl.ToLowerInvariant.EndsWith(mapping.@htmlUrl.ToLowerInvariant) _
                       Order By mapping.@order Ascending _
                       Select mapping.@silverlightUrl

        Return FormatUrl(htmlUrl, mappings)


    End Function



    Public Overrides Function GetHtmlSiteMapEntries() As List(Of PageMappingInfo)

        Dim xdoc = XDocument.Load(GetFilePath())

        Dim mappings = From mapping In xdoc.<PageMappings>.<PageMapping> _
                       Order By mapping.@order Ascending _
                       Select New PageMappingInfo() _
                       With {.ChangeFrequency = mapping.@changeFrequency, _
                             .HtmlUrl = mapping.@htmlUrl, _
                             .LastModified = mapping.@lastModified, _
                             .Order = mapping.@order, _
                             .Priority = mapping.@priority, _
                             .SilverlightUrl = mapping.@silverlightUrl}

        Return mappings.ToList

    End Function

End Class
